package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.entity.AlertRule;
import com.zzyl.mapper.AlertRuleMapper;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author sjqn
 * @date 2023/11/19
 */
@Service
public class AlertRuleServiceImpl implements AlertRuleService {


    @Autowired
    private AlertRuleMapper alertRuleMapper;

    /**
     * 新增报警规则
     * @param alertRuleDto
     */
    @Override
    public void createRule(AlertRuleDto alertRuleDto) {
        AlertRule alertRule = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertRule.setProductId(alertRuleDto.getProductKey());
        alertRule.setRelatedDevice(alertRuleDto.getDeviceId());//设备id
        alertRuleMapper.insert(alertRule);
    }

    /**
     * 分页条件查询
     * @param pageNum
     * @param pageSize
     * @param alertRuleName
     * @param productKey
     * @param functionName
     * @return
     */
    @Override
    public PageResponse<AlertRuleVo> getAlertRulePage(Integer pageNum, Integer pageSize, String alertRuleName, String productKey, String functionName) {

        PageHelper.startPage(pageNum,pageSize);
        Page<AlertRuleVo> page = alertRuleMapper.page(alertRuleName, productKey, functionName);
        //添加提示
        page.getResult().forEach(alertRuleVo -> {

            //ThingModelPropertyDeviceValue <60.0持续触发3个周期时发生报警
            StringBuilder sb = new StringBuilder();
            sb.append("ThingModelPropertyDeviceValue").append(alertRuleVo.getOperator())
                    .append(alertRuleVo.getValue()).append("持续触发").append(alertRuleVo.getDuration()).append("个周期时发生报警");

            alertRuleVo.setRules(sb.toString());
            alertRuleVo.setDeviceId(alertRuleVo.getRelatedDevice());
            alertRuleVo.setProductKey(alertRuleVo.getProductId());
        });

        return PageResponse.of(page,AlertRuleVo.class);
    }

    @Override
    public void deleteAlertRule(Long id) {
        alertRuleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateAlertRule(Long id, AlertRuleDto alertRuleDto) {
        AlertRule alertRule = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertRule.setProductId(alertRuleDto.getProductKey());
        alertRule.setRelatedDevice(alertRuleDto.getDeviceId());
        alertRuleMapper.updateByPrimaryKeySelective(alertRule);
    }

    @Override
    public AlertRuleVo readAlertRule(Long id) {
        AlertRule alertRule = alertRuleMapper.selectByPrimaryKey(id);
        AlertRuleVo alertRuleVo = BeanUtil.toBean(alertRule, AlertRuleVo.class);

        alertRuleVo.setProductKey(alertRule.getProductId());
        alertRuleVo.setDeviceId(alertRule.getRelatedDevice());
        return alertRuleVo;
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        alertRuleMapper.updateStatus(id, status);
    }


}
